import { modelOptions, prop } from '@typegoose/typegoose';
import { ApiProperty } from '@nestjs/swagger';
import { hashSync } from 'bcryptjs';

@modelOptions({
  schemaOptions: {
    timestamps: true,
  },
})
export class User {
  @ApiProperty({ description: '用户名', example: 'test' })
  @prop({ required: true, unique: true })
  username: string;

  @ApiProperty({ description: '密码', example: '1234' })
  @prop({
    required: true,
    select: false,
    get(val) {
      return val;
    },
    set(val) {
      return val ? hashSync(val) : val;
    },
  })
  password: string;

  @ApiProperty({ description: '账号权限', example: '1' })
  @prop({ required: true, default: 1 })
  grant: number;

  @ApiProperty({ description: '电话', example: '1234' })
  @prop()
  tel: number;

  @ApiProperty({ description: '地址', example: 'xxx省xxx市xxx' })
  @prop()
  address: string;
}
